参数替换

${param}

  • 避免参数后面的字符导致的冲突
  • 访问>=10 的位置参数

${string:position}

  • position的位置开始截取

${string:position:length}

  • position的位置开始截取长度为length

${string#substring}

  • 从前往后,截取最短匹配substring

${string##substrng}

  • 从前往后,截取最长匹配substring

${string%substring}

  • 从后往前,截取最短匹配substring

${string%%substrng}

  • 从后往前,截取最长匹配substring

${string/substring/replacement}

  • 替换第一个匹配substring的为replacement

${string//substring/replacement}

  • 替换所有匹配substring的为replacement

${string/#substring/replacement}

  • 如果substring能匹配首部,就替换成replacement

${string/%substring/replacement}

  • 如果substring能匹配尾部,就替换成replacement

案例解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env bash

hello="this is a hello from code ode"

## 变量的长度
echo ${#hello}

## 从截取从10开始的子串
echo ${hello:10}

## 从10开始截取长度为5的子串
echo ${hello:10:5}

## 从前往后截掉最短的*is的匹配
echo ${hello#*is}

## 从前往后截掉最长的*is的匹配
echo ${hello##*is}

## 从后往前截掉最短的ode*的匹配
echo ${hello%ode*}

## 从后往前截掉最长的ode*的匹配
echo ${hello%%ode*}

## 把第一个is换成are
echo ${hello/is/are}

## 把所有is换成are
echo ${hello//is/are}

## 从前往后匹配this的话就替换成THIS
echo ${hello/#this/THIS}

## 从后往前匹配ode的话就替换成ODE
echo ${hello/%ode/ODE}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/env bash

## 处理逗号分隔的字符串
## 把内容复制一遍并输出

## 把第一个参数中的所有逗号替换成空格,便于后面的循环
arg_info=${1//,/ }

result=""
##
for arg in $arg_info
do
result="$result,$arg$arg"
done

## 去掉第一个逗号并输出
echo ${result#?}

最后更新: 2022年03月02日 03:32

原始链接: http://rawbin-.github.io/language/shell/2018-09-22-shell-string/

× 赞赏这个人~
打赏二维码